`
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-using-strategy/
`

/**
 * @param {number[]} prices
 * @param {number[]} strategy
 * @param {number} k
 * @return {number}
 */
var maxProfit = function (prices, strategy, k) {
  const n = prices.length

  // 计算没调整策略前的利润
  let total = 0
  for (let i = 0; i < n; i++) {
    total += prices[i] * strategy[i]
  }

  let res = total
  let left = 0, right = 0

  // 维护一个长度为 k 的滑动窗口
  while (right < n) {
    // 新进入窗口的策略都调整为卖出 1，raw -> 1
    if (strategy[right] !== 1) {
      // 减去之前的策略，加上调整的策略
      total -= (strategy[right] - 1) * prices[right]
    }
    right++

    if (right - left > k / 2) {
      // 进入前半段的策略调整为 0，1 -> 0
      total -= prices[right - k / 2 - 1]
    }

    if (right - left >= k) {
      // 更新答案
      res = Math.max(res, total)
      // 退出窗口的策略调整为原本的策略，0 -> raw
      if (strategy[left] !== 0) {
        total += prices[left] * strategy[left]
      }
      left++
    }
  }

  return res
};